//
//  LoginRegisterView2.m
//  Test
//
//  Created by jingyao on 2017/12/20.
//  Copyright © 2017年 jingyao. All rights reserved.
//

#import "LoginRegisterView2.h"
#import "Global.h"

@interface LoginRegisterView2 ()
@property (nonatomic, assign) Boolean isOpened;
@end

//获取1积分需要消费的金额
#define COST_PER_POINT_ZHAOSHANG 22.0f
#define COST_PER_POINT_JIAOTONG 1.1f
#define COST_PER_POINT_ZHONGXIN 0.55f
#define COST_PER_POINT_ZHADA    1.1f

//每消费1元实际损失金额
//刷卡手续费用 0.55% + 3
//每笔计算为5k 那么金额为30.5元 所以手续费实际金额为 0.61%
#define REAL_PAY_ONECOST (0.61L * 0.01)

@implementation LoginRegisterView2


- (void)imgViewTapped {
    NSLog(@"切换密码图片点击事件已经捕捉到");
    
    if (self.isOpened) {
        self.pswdShowImgView.image = [UIImage imageNamed:@"btn_password hides"];
        self.pswdTextField.secureTextEntry = YES;
        self.isOpened = NO;
    }else {
        self.pswdShowImgView.image = [UIImage imageNamed:@"btn_password display"];
        self.pswdTextField.secureTextEntry = NO;
        self.isOpened = YES;
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    
    self.signInView.layer.cornerRadius = 10.0f;
    self.signInView.layer.masksToBounds = YES;
    self.signUpView.layer.cornerRadius = 10.0f;
    self.signUpView.layer.masksToBounds = YES;
    self.bottomView.layer.cornerRadius = 10.0f;
    self.bottomView.layer.masksToBounds = YES;
    
    self.pswdShowImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgViewTapped)];
    [self.pswdShowImgView addGestureRecognizer:tap];
    
    //以一千积分为例，一万积分兑换多少价值的金额才不亏？
    
    //招商银行
    //理论最少兑换金额为：122.0
    //实际需要兑换金额为：134.2
    CGFloat value_ZHAOSHANG = 1000 * COST_PER_POINT_ZHAOSHANG * REAL_PAY_ONECOST;
    
    //交通银行
    //理论最少兑换金额为：6.1
    //实际需要兑换金额为：6.71
    CGFloat value_JIAOTONG = 1000 * COST_PER_POINT_ZHAOSHANG * REAL_PAY_ONECOST;
    
    //中信银行
    //理论最少兑换金额为：3.05
    //实际需要兑换金额为：3.355
    CGFloat value_ZHONGXIN = 1000 * COST_PER_POINT_ZHONGXIN * REAL_PAY_ONECOST;
    
    
    //渣打银行
    //理论最少兑换金额为：6.1
    //实际需要兑换金额为：6.71
    CGFloat value_ZHADA = 1000 * COST_PER_POINT_ZHADA * REAL_PAY_ONECOST;
}


- (IBAction)loginAction:(id)sender {
    [self requestForLogin];
}

#pragma mark - 网络请求方法

- (void)requestForLogin {
    NSURL *url = [NSURL URLWithString:@"http://172.105.198.110:8099/api/signin"];//以这个为准而非文档
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    mRequest.HTTPMethod = @"POST";
    
    //NSString *testEmail = @"henderson_test_local@123.com";//测试账号
    //NSString *testPswd = @"123456";
    
    NSString *tenantEmailStr = @"tenant@henderson.com";//tenant账号，3个窗户9个窗帘,演示账号
    NSString *tenantPswdStr = @"123456";
    
    NSString *fmadminEmailStr = @"fm@henderson.com";//整个楼的窗户与窗帘，演示账号
    NSString *fmadminPswdStr = @"123456";
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:tenantEmailStr, @"email", tenantPswdStr, @"password", nil];
    NSLog(@"请求登录接口的dict是：%@", dict);
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    mRequest.HTTPBody = data;
    [mRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mRequest completionHandler:
                                      ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                          
                                          if (error) {
                                              NSLog(@"error是:%@", error);
                                              return ;
                                          }
                                          
                                          NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
                                          //NSLog(@"response是:%@", response);
                                          
                                          NSError *jsonError;
                                          NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error: &jsonError];
                                          
                                          if (jsonError) {
                                              NSLog(@"数据转换出错了，jsonError是：%@", jsonError);
                                              return;
                                          }
                                          
                                          NSInteger code = [[dataDic objectForKey:@"code"] integerValue];
                                          if (code == 0) {//唯一成功的情况
                                              /*
                                              NSDictionary *userDict = [dataDic objectForKey:@"user"];//提取user
                                              NSArray *devicesArray = [userDict objectForKey:@"devices"];//提取设备列表
                                              for (int i = 0; i < devicesArray.count; i++) {
                                                  NSDictionary *deviceDict = devicesArray[i];//提取device
                                                  NSString *deviceId = [deviceDict objectForKey:@"id"];//提取deviceID，用于请求查询设备状态接口
                                                  NSString *deviceType = [deviceDict objectForKey:@"type"];//提取设备类型，用于判断加载哪个页面
                                              }*/
                                              NSString *token = [dataDic objectForKey:@"token"];
                                              [Global saveTokenWithToken:token];
                                              NSString *getToken = [Global getToken];
                                              NSLog(@"取出来的token是：%@", getToken);
                                              
                                              //发送通知
                                              NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
                                              [center postNotificationName:@"loginNotification" object:token];
                                          }else {
                                              NSLog(@"第一个code不是0，是：%ld", (long)code);
                                              //显示提示错误的hud
                                          }
                                      }];
    [dataTask resume];
}
@end
